//
//  TYBaseWebViewVC.m
//
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "TYBaseWebViewVC.h"
#import "UIWebView+AFNetworking.h"
#define GJPropertyKepPath (@"fractionCompleted")

@interface TYBaseWebViewVC ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, strong) UIProgressView *progressView;
/** 失败View*/
@property(nonatomic,strong)UIView *emptyView;



@end

@implementation TYBaseWebViewVC
#pragma mark - LifeCycle
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self  = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createContentView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self removeProgressView];

}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)dealloc
{
    NSLog(@"释放%s",__func__);
}
#pragma mark - 构建UI
/** 创建*/
-(void)createContentView
{
    self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 62, Screen_Width, 2)];
    self.progressView.progressViewStyle = UIProgressViewStyleBar;
    self.progressView.progressTintColor = [UIColor grayColor];
    [self.progressView setProgress:0.6 animated:YES];
    [self.navigationController.view addSubview:self.progressView];
    

    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, Screen_Width, Screen_Height-64)];
    self.webView.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:(self.loadUrl)]];
    __weak __typeof(self)tempSelf = self;
    
    NSProgress *progres = nil;
    [self.webView loadRequest:request progress:&progres success:^NSString *(NSHTTPURLResponse *response, NSString *HTML)
    {
         NSString *title = [tempSelf.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
         if (title.length)
             tempSelf.title = title;
         [tempSelf setProgress:1];
        [progres removeObserver:tempSelf forKeyPath:GJPropertyKepPath];
         return HTML;
        
     } failure:^(NSError *error)
    {
         [tempSelf setProgress:-2];
     }];
    
    [progres addObserver:self forKeyPath:GJPropertyKepPath options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

}

-(UIView *)emptyView
{
    if (nil == _emptyView)
    {
        _emptyView = [[UIView alloc]initWithFrame:self.webView.bounds];
        UILabel *descLabel = [[UILabel alloc]initWithFrame:_emptyView.bounds];
        descLabel.text = @"加载失败";
        descLabel.font = FontCustomSize(28.f);
        descLabel.textAlignment = NSTextAlignmentCenter;
        descLabel.textColor = [UIColor grayColor];
        [_emptyView addSubview:descLabel];
        
    }
    return _emptyView;
}


#pragma mark - Business Method
- (void)setProgress:(CGFloat)progress
{
    if (progress==-2)
    {
        /** 加载失败处理*/
        [self.view addSubview:self.emptyView];
        [self.progressView removeFromSuperview];
    }
    if (progress < 0.6) {
        return;
    }
    [self.progressView setProgress:progress animated:YES];
    if (progress==1) {
        [self performSelector:@selector(removeProgressView) withObject:nil afterDelay:0.8];
        return;
    }
    
}

- (void)removeProgressView
{
    [self.progressView removeFromSuperview];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:GJPropertyKepPath] && [object isKindOfClass:[NSProgress class]]) {
        NSProgress *progress = (NSProgress *)object;
        [self setProgress:progress.completedUnitCount/progress.totalUnitCount];
    }
}



@end
